import { Effect } from "dva"
import { Reducer } from "umi"
import { getMenuList } from "./service"

export type ModelState = {
    list:[];
    item:any;
}

export type TEffect = Record<'fetchList', Effect>
export type TReducer = Record<'setList' | 'setItem', Reducer<ModelState>>

export default <ModelType<ModelState, TEffect, TReducer>>{
    namespace: 'menu',
    state: {
        list: [],
        item: {}
    },
    effects: {
        *fetchList(_, { call, put }) {
            const res = yield call(getMenuList);
            yield put({
                type: 'setList',
                payload: res.data.list
            });
        }
    },
    reducers: {
        setList(state, { payload }) {
            return {
                ...state,
                list: payload
            }
        },
        setItem(state, { payload }) {
            console.log(payload)
            return {
                ...state,
                item: payload
            }
        }
    }
}